var config = require('../../config.js');
var i=0;
var  name='';

/**
 * 根据列表获取收藏情况
 * objcount  主要是判断当前的收藏是否加载完成
 */
function findFavorer_a(name,obj,objcount) {
  wx.request({
    url: config.materials + '/search/findByMaterialIdAndFavorerUsername',
    data: {
      materialId: obj.id,
      username: name
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      if (data) {
        obj.favorite = '1';//表示收藏
      } else {
        obj.favorite = '0';//表示未收藏
      }
      objcount.count = objcount.count+1;
    }
  });
}
/**
 * 根据name和id获取收藏情况
 */
function findFavorer_p(name,id,func){
  wx.request({
    url: config.materials + '/search/findByMaterialIdAndFavorerUsername',
    data: {
      materialId: id,
      username: name
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      typeof func =="function"&&func(res);
    }
  });
}



/**
 * 收藏或是移除收藏
 * id 
 * action 为add或remove,cb  回掉函数
 */
function add_rem_favorite(id, name, action, cb) {
  wx.request({
    url: config.materials + '/' + id + '/favor',
    data: {
      username: name,
      action: action
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      if ("success" == res.data) {
        //render_page(id,);
        console.log('add_rem_favorite::'+new Date().getTime());
        typeof cb=="function"&&cb();
      }
    }
  });
}

function change_tab_list(id,status,tab_list,page){
  var list = tab_list.list;
  if (list) {
    for (var i = 0; i < list.length; i++) {
      if (list[i].id == id) {
        list[i].favorite = status;
      }
    }
    page.setData({
      list: list
    });
  }
  
}

//新增或更新 播放记录
function add_up_record(materialId, username, lastSecond){
  wx.request({
    url: config.materials + '/' + materialId+'/view',
    data: {
      username: username,
      lastSecond: lastSecond
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
     
    }
  });
}
//根据id获取lastSecond
function get_record_id(materialId, username,cb){
  wx.request({
    url: config.materialViews + '/search/findByMaterialIdAndViewerUsername',
    data: {
      materialId: materialId,
      username: username
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      typeof cb=="function"&&cb(res);
    }
  });
}
//根据id获取lastSecond
function set_record_id(materialId, username,obj) {
  wx.request({
    url: config.materialViews + '/search/findByMaterialIdAndViewerUsername',
    data: {
      materialId: materialId,
      username: username
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      if(res.data){
        obj.lastSecond = res.data.lastSecond;
      }else{
        obj.lastSecond = 0;
      }
    }
  });
}

module.exports = {
  findFavorer_a: findFavorer_a,
  add_rem_favorite: add_rem_favorite,
  findFavorer_p: findFavorer_p,
  change_tab_list: change_tab_list,
  add_up_record: add_up_record,
  get_record_id: get_record_id,
  set_record_id: set_record_id
}  